package com.hddf.project.interceptor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.codehaus.jackson.JsonNode;

import com.hddf.project.pojos.User;
import com.hddf.project.util.Stct;
import com.hddf.project.util.http.JsonHttp;
import com.hddf.project.util.http.JsonUtils;

public class GetData {

	/**
	 * 检查用户
	 * 
	 * @param token
	 * @param time
	 * @return
	 */
	public Map<String, Object> checkUser(String token, String time, String uri) {
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair("token", token));
			params.add(new BasicNameValuePair("time", time));
			params.add(new BasicNameValuePair("uri", uri));

			String result = JsonHttp.getDataFromHTTP(Stct.HTTP_SERVER_CHECK, params); // 连接数据中心接口，得到返回值
			if (null != result && 0 < result.length()) {
				JsonNode jsonNode = JsonUtils.getInstance().readTree(result); // 编译数据中心接口返回值
				String resultState = jsonNode.get("state").asText();
				if ("ok".equals(resultState)) {
					map.put("state", "success");
					String webtoken = jsonNode.get("webtoken").asText();
					map.put("webtoken", webtoken);
					String userStr = jsonNode.get("user").toString();
					User user = (User) JsonUtils.toJavaBean(userStr, User.class);
					map.put("user", user);
					map.put("msg", "");
					String permission = jsonNode.get("permission").asText();
					map.put("permission", permission);
				} else {
					map.put("state", "error");
					map.put("msg", jsonNode.get("msg").asText());
				}
			}
			return map;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 检查权限
	 * 
	 * @param token
	 * @param time
	 * @return
	 */
	public Map<String, Object> checkPermission(String token, String time, String uri) {
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair("token", token));
			params.add(new BasicNameValuePair("time", time));
			params.add(new BasicNameValuePair("uri", uri));

			String result = JsonHttp.getDataFromHTTP(Stct.HTTP_SERVER_CHECK_UPLOAD, params); // 连接数据中心接口，得到返回值
			if (null != result && 0 < result.length()) {
				JsonNode jsonNode = JsonUtils.getInstance().readTree(result); // 编译数据中心接口返回值
				String resultState = jsonNode.get("state").asText();
				if ("ok".equals(resultState)) {
					map.put("state", "success");
					String permission = jsonNode.get("permission").asText();
					map.put("permission", permission);
					map.put("msg", "");
				} else {
					map.put("state", "error");
					map.put("msg", jsonNode.get("msg").asText());
				}
			}
			return map;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}